package com.hx.petsever.controller;


import com.hx.petsever.config.Result;
import com.hx.petsever.entity.Orders;
import com.hx.petsever.entity.userOrder;
import com.hx.petsever.service.OrderService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

@RestController
@RequestMapping("/order")
@CrossOrigin(origins="http://localhost:9999")
public class OrderController {

    @Autowired
    OrderService orderService;

    @RequestMapping("/addone")
    public Result Addone(@Param("id") String id , @Param("name") String name ,
                         @Param("address")String address , @Param("phone") String phone ,
                         @Param("state") Integer state, @Param("goodname") String goodname , @Param("price") String price,
                        @Param("userid") String userid,@Param("number") Integer number,@Param("goodid") Integer goodid)
    {
        Result<Object> result = new Result<>();
        Orders orders = new Orders(id, name, address, phone, state, goodname, price,number,goodid);
        userOrder userOrder = new userOrder(userid,id);
        System.out.println(orders);
        HashMap<String , Object> map = orderService.InsertOne(orders,userOrder);
        if((int)map.get("state") == -1)
            result.fail(map.get("msg").toString());
        else result.success(map.get("msg").toString());
        return result;
    }

    @RequestMapping("getByState")
    public Result GetOrderByState(@Param("userid") String userid ,@Param("state") Integer state)
    {
        Result<Object> result = new Result<>();
        HashMap<String, Object> orderByState = orderService.getOrderByState(userid, state);
        result.setData(orderByState.get("data"));
        result.success();
        return result;
    }

    @RequestMapping("/buy")
    public Result BuyOne(@Param("userid") String userid , @Param("id") String id,@Param("price") String price)
    {
        Result<Object> result = new Result<>();
        HashMap map = orderService.buyOne(userid, id, price);
        if(map.get("state").equals("0"))
        {
            result.fail(map.get("msg").toString());
        }else{
            result.success(map.get("msg").toString());
        }
        return result;
    }

    @RequestMapping("/cancelOrder")
    public Result cancelOrder( @Param("id") String id)
    {
        Result<Object> result = new Result<>();
        orderService.cancelOrder(id);
        return result;
    }

    @RequestMapping("/confirmOrder")
    public Result confirmOrder( @Param("id") String id)
    {
        Result<Object> result = new Result<>();
        orderService.confirmOrder(id);
        return result;
    }

    @RequestMapping("/applyCancelOrder")
    public Result applyCancelOrder( @Param("id") String id , @Param("reason") String reason)
    {
        Result<Object> result = new Result<>();
        orderService.applyCancelOrder(id,reason);
        return result;
    }

    @RequestMapping("/getAll")
    public Result getAll()
    {
        Result<Object> result = new Result<>();
        HashMap all = orderService.getAll();
        result.setData(all.get("data"));
        return result;
    }

    @RequestMapping("/deliver")
    public Result deliver( @Param("id") String id)
    {
        Result<Object> result = new Result<>();
        orderService.deliver(id);
        result.success();
        return result;
    }

    @RequestMapping("/getReason")
    public Result getReason(@Param("id") String id)
    {
        Result<Object> result = new Result<>();
        String reason = orderService.getReason(id);
        result.setData(reason);
        return result;
    }

    @RequestMapping("/backOrder")
    public Result backOrder(@Param("id") String id)
    {
        Result<Object> result = new Result<>();
        orderService.backOrder(id);
        return  result;
    }

    @RequestMapping("/assess")
    public Result Assess(@Param("goodid") Integer goodid , @Param("orderid") String orderid , @Param("userid") String userid , @Param("assess") String assess)
    {
        Result<Object> result = new Result<>();
        System.out.println("评价信息" + goodid + ":" + orderid + ":" + userid + ":" + assess);
        orderService.Assess(goodid,orderid,userid,assess);
        return result;
    }

    @RequestMapping("/haveAssess")
    public Result haveAssess(@Param("orderid") String orderid) {
        Result<Object> result = new Result<>();
        HashMap map = orderService.haveAssess(orderid);
        Integer state = (Integer) map.get("state");
        if(state == 1)
            result.success();
        else result.fail();
        return  result;
    }
}
